package main

import "fmt"

func main() {
	fmt.Printf("run test 1\n")
	//test1()
	fmt.Printf("Test completed\n")
	//test2()
	fmt.Printf("Test 2 completed\n")

	test3()
	fmt.Printf("Test 3 completed\n")
}

func badCall() {
	panic("bad end")
}

func test1() {
	defer func() {
		fmt.Println("defer 1-1")
		if e := recover(); e != nil {
			fmt.Printf("Panicing %s\n", e)
		}
		fmt.Println("defer 1-2")
	}()
	defer func() {
		fmt.Println("defer 2")
	}()
	badCall()
	fmt.Printf("After bad call\n")
}

func test2() {
	for i := 0; i < 10; i++ {
		func() {
			defer func() {
				if r := recover(); r != nil {
					fmt.Println(r)
				}
			}()

			if i == 2 {
				panic("got 2")
			} else {
				fmt.Println(i)
			}
		}()
	}
}

func test3() {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println(r)
		}
	}()
	defer func() {
		fmt.Println(1111111)
	}()
	for i := 0; i < 10; i++ {
		if i == 2 {
			panic("got 2")
		} else {
			fmt.Println(i)
		}
	}
}
